#include "stdafx.h"
#include "test_common.h"
#include <windows.h>



void compareBencodeType( const BENCODE_SHARED_TYPE & type1, const BENCODE_SHARED_TYPE & type2 )
{
	BOOST_REQUIRE_EQUAL( type1->GetType(), type2->GetType() );

	switch( type1->GetType() )
	{
	case STRING:
		{
			BencodeString * pRawType1 = dynamic_cast<BencodeString *>( type1.get() );
			BencodeString * pRawType2 = dynamic_cast<BencodeString *>( type2.get() );

			BOOST_REQUIRE_EQUAL( 0, strcmp( pRawType1->GetValue().c_str(), pRawType2->GetValue().c_str() ) );
		}
		break;
	case INTEGER:
		{
			BencodeInteger * pRawType1 = dynamic_cast<BencodeInteger *>( type1.get() );
			BencodeInteger * pRawType2 = dynamic_cast<BencodeInteger *>( type2.get() );

			BOOST_REQUIRE_EQUAL( pRawType1->GetValue(), pRawType2->GetValue() );
		}
		break;
	case LIST:
		{
			BencodeList * pRawType1 = dynamic_cast<BencodeList *>( type1.get() );
			BencodeList * pRawType2 = dynamic_cast<BencodeList *>( type2.get() );

			compareList( pRawType1->GetValue(), pRawType2->GetValue() );
		}
		break;
	case DICTIONARY:
		{
		}
		break;
	default:
		BOOST_FAIL("compareBencodeType ivalid bencode type");
		break;
	}
}

void compareList( const BENCODE_LIST & list1, const BENCODE_LIST & list2 )
{
	BOOST_REQUIRE_EQUAL( list1.size(), list2.size() );

	BENCODE_LIST::const_iterator it1 = list1.begin();
	BENCODE_LIST::const_iterator end_it1 = list1.end();

	BENCODE_LIST::const_iterator it2 = list2.begin();

	for( ; it1 != end_it1 ; ++it1, ++it2 )
		compareBencodeType( *it1, *it2 );
}

void compareDic(const BENCODE_DICTIONARY & dic1, const BENCODE_DICTIONARY & dic2 )
{
	BOOST_REQUIRE_EQUAL( dic1.size(), dic2.size() );

	BENCODE_DICTIONARY::const_iterator it1 = dic1.begin();
	BENCODE_DICTIONARY::const_iterator end_it1 = dic1.end();

	BENCODE_DICTIONARY::const_iterator it2 = dic2.begin();

	for( ; it1 != end_it1; ++it1, ++it2 )
	{
		compareBencodeType( it1->first, it2->first );
		compareBencodeType( it1->second, it2->second );
	}
}


void checkString( const char * value, const BENCODE_SHARED_TYPE & type)
{
	BOOST_REQUIRE_EQUAL( STRING, type->GetType() );

	const BencodeString * pString = dynamic_cast<const BencodeString *>(type.get() );
	
	std::string test;
	test = value;
	test += " : ";
	test += pString->GetValue();
	::OutputDebugStringA( test.c_str() );
	BOOST_REQUIRE_EQUAL( 0, strcmp( value, pString->GetValue().c_str() ));
}

void checkStringUTF8( const wchar_t * value, const BENCODE_SHARED_TYPE & type)
{
	BOOST_REQUIRE_EQUAL( STRING, type->GetType() );

	const BencodeString * pString = dynamic_cast<const BencodeString *>(type.get() );
	const std::string & str = pString->GetValue();

	int length = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str() , str.size(), NULL, 0 );
	std::vector<wchar_t> buffer( length+1, L'\0' );
	MultiByteToWideChar(CP_UTF8, 0, &str[0] , str.size(), &buffer[0], length+1 );

	BOOST_REQUIRE_EQUAL( 0, wcscmp( value, &buffer[0] ) );
}

void checkInteger( int value, const BENCODE_SHARED_TYPE & type)
{
	BOOST_REQUIRE_EQUAL( INTEGER, type->GetType() );

	const BencodeInteger * pInteger = dynamic_cast<const BencodeInteger *>(type.get() );
	BOOST_REQUIRE_EQUAL( value, pInteger->GetValue() );
}